<template>
  <view class="container">
    <!-- 自定义导航栏 -->
    <view class="navbar">
      <view class="navbar-left" @click="goBack">
        <text class="iconfont navbar-icon">&#xe65d;</text>
      </view>
      <text class="navbar-title">修改密码</text>
      <view class="navbar-right"></view>
    </view>

    <!-- 表单区域 -->
    <view class="form-section">
      <!-- 当前密码 -->
      <view class="form-group">
        <text class="form-label">当前密码</text>
        <view class="form-item">
          <input 
            class="form-input" 
            v-model="formData.oldPassword" 
            placeholder="请输入当前密码"
            :password="!showOldPassword"
            maxlength="50"
            @input="clearError"
          />
          <text 
            class="password-toggle" 
            @click="toggleOldPassword"
          >
            {{ showOldPassword ? '👁️' : '👁️‍🗨️' }}
          </text>
        </view>
      </view>

      <!-- 新密码 -->
      <view class="form-group">
        <text class="form-label">新密码</text>
        <view class="form-item">
          <input 
            class="form-input" 
            v-model="formData.newPassword" 
            placeholder="请输入新密码(6-20位)"
            :password="!showNewPassword"
            maxlength="50"
            @input="clearError"
            @blur="validateNewPassword"
          />
          <text 
            class="password-toggle" 
            @click="toggleNewPassword"
          >
            {{ showNewPassword ? '👁️' : '👁️‍🗨️' }}
          </text>
        </view>
        <text v-if="errors.newPassword" class="error-text">{{ errors.newPassword }}</text>
      </view>

      <!-- 确认新密码 -->
      <view class="form-group">
        <text class="form-label">确认新密码</text>
        <view class="form-item">
          <input 
            class="form-input" 
            v-model="formData.confirmPassword" 
            placeholder="请再次输入新密码"
            :password="!showConfirmPassword"
            maxlength="50"
            @input="clearError"
            @blur="validateConfirmPassword"
          />
          <text 
            class="password-toggle" 
            @click="toggleConfirmPassword"
          >
            {{ showConfirmPassword ? '👁️' : '👁️‍🗨️' }}
          </text>
        </view>
        <text v-if="errors.confirmPassword" class="error-text">{{ errors.confirmPassword }}</text>
      </view>

      <!-- 错误提示 -->
      <view v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </view>

      <!-- 密码要求提示 -->
      <view class="password-tips">
        <text class="tips-title">密码要求：</text>
        <view class="tips-list">
          <text class="tips-item">• 长度6-20位</text>
          <text class="tips-item">• 建议包含字母、数字</text>
          <text class="tips-item">• 不能与当前密码相同</text>
        </view>
      </view>
    </view>

    <!-- 提交按钮 -->
    <view class="button-section">
      <button 
        class="submit-btn" 
        :class="{ disabled: !canSubmit || loading }"
        @click="handleSubmit"
        :disabled="!canSubmit || loading"
      >
        {{ loading ? '修改中...' : '确认修改' }}
      </button>
    </view>

    <!-- 安全提示 -->
    <view class="security-tips">
      <view class="tips-header">
        <text class="security-icon">🔒</text>
        <text class="tips-title">安全提示</text>
      </view>
      <text class="tips-content">修改密码后需要重新登录，请确保记住新密码</text>
    </view>
  </view>
</template>

<script>
import { request } from '@/utils/request.js';
import { logout } from '@/utils/auth.js';

export default {
  data() {
    return {
      loading: false,
      showOldPassword: false,
      showNewPassword: false,
      showConfirmPassword: false,
      errorMessage: '',
      
      formData: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      
      errors: {
        newPassword: '',
        confirmPassword: ''
      }
    }
  },
  
  computed: {
    canSubmit() {
      return this.formData.oldPassword.trim() &&
             this.formData.newPassword.length >= 6 &&
             this.formData.confirmPassword &&
             this.formData.newPassword === this.formData.confirmPassword &&
             !this.errors.newPassword &&
             !this.errors.confirmPassword;
    }
  },
  
  methods: {
    // 清除错误信息
    clearError() {
      this.errorMessage = '';
    },
    
    // 切换密码显示状态
    toggleOldPassword() {
      this.showOldPassword = !this.showOldPassword;
    },
    
    toggleNewPassword() {
      this.showNewPassword = !this.showNewPassword;
    },
    
    toggleConfirmPassword() {
      this.showConfirmPassword = !this.showConfirmPassword;
    },
    
    // 验证新密码
    validateNewPassword() {
      const newPassword = this.formData.newPassword;
      
      if (!newPassword) {
        this.errors.newPassword = '请输入新密码';
        return false;
      }
      
      if (newPassword.length < 6) {
        this.errors.newPassword = '密码长度至少6位';
        return false;
      }
      
      if (newPassword.length > 20) {
        this.errors.newPassword = '密码长度不能超过20位';
        return false;
      }
      
      if (newPassword === this.formData.oldPassword) {
        this.errors.newPassword = '新密码不能与当前密码相同';
        return false;
      }
      
      this.errors.newPassword = '';
      
      // 如果确认密码已输入，重新验证确认密码
      if (this.formData.confirmPassword) {
        this.validateConfirmPassword();
      }
      
      return true;
    },
    
    // 验证确认密码
    validateConfirmPassword() {
      const confirmPassword = this.formData.confirmPassword;
      
      if (!confirmPassword) {
        this.errors.confirmPassword = '请确认新密码';
        return false;
      }
      
      if (confirmPassword !== this.formData.newPassword) {
        this.errors.confirmPassword = '两次输入的密码不一致';
        return false;
      }
      
      this.errors.confirmPassword = '';
      return true;
    },
    
    // 提交修改
    async handleSubmit() {
      if (!this.canSubmit || this.loading) return;
      
      // 重新验证所有字段
      if (!this.validateNewPassword() || !this.validateConfirmPassword()) {
        return;
      }
      
      this.loading = true;
      this.clearError();
      
      try {
        const response = await request({
          url: '/api/user/change-password',
          method: 'POST',
          data: {
            oldPassword: this.formData.oldPassword,
            newPassword: this.formData.newPassword,
            confirmPassword: this.formData.confirmPassword
          }
        });
        
        if (response.success) {
          uni.showModal({
            title: '修改成功',
            content: '密码修改成功，需要重新登录',
            showCancel: false,
            success: () => {
              // 清除本地数据并跳转到登录页
              logout();
              uni.reLaunch({
                url: '/pages/login/index'
              });
            }
          });
        } else {
          this.errorMessage = response.message || '密码修改失败';
        }
        
      } catch (error) {
        console.error('密码修改失败:', error);
        this.errorMessage = error.message || '网络错误，请重试';
      } finally {
        this.loading = false;
      }
    },
    
    // 返回上一页
    goBack() {
      uni.navigateBack();
    }
  }
}
</script>

<style scoped>
/* 基本样式 */
.container {
  background: #f5f5f5;
  min-height: 100vh;
}

/* 自定义导航栏 */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid #eee;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
}

.navbar-left,
.navbar-right {
  width: 60px;
}

.navbar-icon {
  font-size: 20px;
  color: #333;
}

.navbar-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  text-align: center;
}

/* 表单区域 */
.form-section {
  margin-top: 80px;
  padding: 0 16px;
}

.form-group {
  background: #fff;
  border-radius: 12px;
  margin-bottom: 16px;
  padding: 16px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin-bottom: 12px;
}

.form-item {
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 0 16px;
  border: 2px solid transparent;
  transition: all 0.3s;
}

.form-item:focus-within {
  border-color: #667eea;
  background: #fff;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.form-input {
  flex: 1;
  height: 50px;
  border: none;
  background: transparent;
  font-size: 16px;
  color: #333;
  outline: none;
}

.password-toggle {
  font-size: 18px;
  padding: 8px;
  cursor: pointer;
  opacity: 0.6;
  transition: opacity 0.3s;
}

.password-toggle:hover {
  opacity: 1;
}

/* 错误提示 */
.error-text {
  color: #ff4757;
  font-size: 12px;
  margin-top: 8px;
  margin-left: 4px;
}

.error-message {
  background: #fff5f5;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 16px;
  color: #dc2626;
  font-size: 14px;
  display: flex;
  align-items: center;
}

.error-message::before {
  content: '⚠️';
  margin-right: 8px;
  font-size: 16px;
}

/* 密码提示 */
.password-tips {
  background: #f0f9ff;
  border: 1px solid #e0f2fe;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 24px;
}

.tips-title {
  font-size: 14px;
  font-weight: 500;
  color: #0369a1;
  margin-bottom: 8px;
  display: block;
}

.tips-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.tips-item {
  font-size: 13px;
  color: #0369a1;
  line-height: 1.4;
}

/* 提交按钮 */
.button-section {
  padding: 0 16px 32px;
}

.submit-btn {
  width: 100%;
  height: 50px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 12px;
  font-size: 16px;
  font-weight: bold;
  transition: all 0.3s;
}

.submit-btn:active {
  transform: translateY(1px);
}

.submit-btn.disabled {
  background: #e5e7eb;
  color: #9ca3af;
  transform: none;
}

/* 安全提示 */
.security-tips {
  background: #fff;
  border-radius: 12px;
  margin: 0 16px 32px;
  padding: 16px;
  border-left: 4px solid #fbbf24;
}

.tips-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.security-icon {
  font-size: 16px;
  margin-right: 8px;
}

.tips-title {
  font-size: 14px;
  font-weight: 500;
  color: #92400e;
}

.tips-content {
  font-size: 13px;
  color: #92400e;
  line-height: 1.5;
}
</style>